<?php

	set_include_path('../140dev/db');
	
	require_once('140dev_config.php');	  
	require_once('db_lib.php');
	
	$oDB = new db;
    
 	if(!$oDB)
  	{
  		die('Could not connect: ' . mysql_error());
  	}
	
	try{
		$aColumns = array('id','ds_rest_name','ds_rest_address','ds_rest_url','ds_rest_email','ds_rest_facebook','ds_rest_twitter','id_rest_tripadvisor','id_rest_atrapalo','id_rest_tenedor','id_rest_restalo','id_rest_salir','id_rest_yelp');

	/* 
	 * Paging
	 */
	$sLimit = "";
	if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
	{
		$sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ) . ", " . intval( $_GET['iDisplayLength'] );
	}
	
	$sWhere = "";
	if ( $_GET['sSearch'] != "" )
	{
			$sWhere .= " AND T1.DS_REST_NAME LIKE '%" . mysql_real_escape_string($_GET['sSearch']) . "%'  ";

	}
	$sWhere = "T1.BL_ACTIVE = 'Y' " . $sWhere;
	
	/*
	 * Ordering
	 */
	/*
	$sOrder = "";
	if ( isset( $_GET['iSortCol_0'] ) )
	{
		$sOrder = "ORDER BY  ";
		for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
		{
			if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
			{
				$sOrder .= "'".$aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."' ".
					($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
			}
		}
		
		$sOrder = substr_replace( $sOrder, "", -2 );
		if ( $sOrder == "ORDER BY" )
		{
			$sOrder = "";
		}
	}
	*/

		$sql = "SELECT T1.ID_RESTAURANT, T1.DS_REST_NAME, T1.DS_REST_ADDRESS, T1.DS_REST_URL, T1.DS_REST_EMAIL, T1.DS_REST_FACEBOOK, T1.DS_REST_TWITTER, ";
		$sql = $sql . "T2.ID_REST_SOURCE as ID_REST_TRIPADVISOR, T3.ID_REST_SOURCE as ID_REST_ATRAPALO, ";
		$sql = $sql . "T4.ID_REST_SOURCE as ID_REST_TENEDOR, T5.ID_REST_SOURCE as ID_REST_RESTALO, T6.ID_REST_SOURCE as ID_REST_SALIR, T7.ID_REST_SOURCE as ID_REST_YELP ";
		$sql = $sql . "FROM MAST_RESTAURANTS T1 ";
		$sql = $sql . "LEFT JOIN MAST_REST_TRIPADVISOR T2 ON T1.ID_RESTAURANT = T2.ID_RESTAURANT ";
		$sql = $sql . "LEFT JOIN MAST_REST_ATRAPALO T3 ON T1.ID_RESTAURANT = T3.ID_RESTAURANT ";
		$sql = $sql . "LEFT JOIN MAST_REST_TENEDOR T4 ON T1.ID_RESTAURANT = T4.ID_RESTAURANT ";
		$sql = $sql . "LEFT JOIN MAST_REST_RESTALO T5 ON T1.ID_RESTAURANT = T5.ID_RESTAURANT ";
		$sql = $sql . "LEFT JOIN MAST_REST_SALIR T6 ON T1.ID_RESTAURANT = T6.ID_RESTAURANT ";
		$sql = $sql . "LEFT JOIN MAST_REST_YELP T7 ON T1.ID_RESTAURANT = T7.ID_RESTAURANT ";
		$sql = $sql . "WHERE ";
		$sql = $sql . $sWhere;
		$sql = $sql . "ORDER BY T1.ID_RESTAURANT ASC ";
		$result_file = $oDB->select($sql);

		$sql = $sql . $sLimit;
		$result = $oDB->select($sql);
		
		/* Data set length after filtering */
		$sQuery = "SELECT FOUND_ROWS()";
		$rResultFilterTotal = $oDB->select($sQuery);
		$aResultFilterTotal = mysqli_fetch_row($rResultFilterTotal);
		$iFilteredTotal = $aResultFilterTotal[0];
	
		/* Total data set length */
		$sQuery = "SELECT COUNT(*) FROM  MAST_RESTAURANTS";
		$rResultTotal =  $oDB->select($sQuery);
		$aResultTotal = mysqli_fetch_row($rResultTotal);
		$iTotal = $aResultTotal[0];

		/*
	 		* Output
	 	*/
		$output = array(
			"sEcho" => intval($_GET['sEcho']),
			"iTotalRecords" => $iTotal,
			"iTotalDisplayRecords" => $iTotal,
			"aaData" => array()
		);

		

		while($aRow = mysqli_fetch_row($result))
		{
			

			$row = array();
			for ( $i=0 ; $i<13 ; $i++ )
			{
				$row[] = $aRow[$i];
			}
			/*
			$result_restaurant = array();
			$result_restaurant['id'] = $row[0];
			$result_restaurant['ds_rest_name'] = $row[1];
			$result_restaurant['ds_rest_address'] = $row[2];
			$result_restaurant['ds_rest_url'] = $row[3];
			$result_restaurant['ds_rest_email'] = $row[4];
			$result_restaurant['ds_rest_facebook'] = $row[5];
			$result_restaurant['ds_rest_twitter'] = $row[6];
			$result_restaurant['id_rest_tripadvisor'] = $row[7];
			$result_restaurant['id_rest_atrapalo'] = $row[8];
			$result_restaurant['id_rest_tenedor'] = $row[9];
			$result_restaurant['id_rest_restalo'] = $row[10];
			$result_restaurant['id_rest_salir'] = $row[11];
			$result_restaurant['id_rest_yelp'] = $row[12];	
			*/
			$output['aaData'][] = $row;
		}	

		$fp = fopen('file.csv', 'w');
		fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF));
		while($aRow = mysqli_fetch_row($result_file))
		{
			fputcsv($fp, $aRow);
		}
		fclose($fp);
		echo json_encode($output);
	}
	catch(Exception $e){
		echo $e;
	}	
	
?>